//! 冲正消息处理器
//! 处理1420冲正请求消息

use log::debug;
use iso8583_rs::iso8583::iso_spec::IsoMsg;
use iso8583_rs::iso8583::IsoError;
use crate::config::{message_types, response_codes};
use crate::services::{ValidationService, ResponseBuilder};
use super::message_handler::{MessageHandler, MessageHandleResult};

/// 冲正消息处理器
pub struct ReversalHandler;

impl ReversalHandler {
    pub fn new() -> Self {
        Self
    }
}

impl MessageHandler for ReversalHandler {
    fn handle(
        &self,
        request_msg: &IsoMsg,
        _raw_msg: &[u8],
        _validation_service: &ValidationService,
    ) -> Result<MessageHandleResult, IsoError> {
        debug!("开始处理冲正请求消息");

        // 冲正消息通常返回拒绝响应
        let response_msg = ResponseBuilder::build_error_response(
            request_msg,
            message_types::REVERSAL_RESPONSE,
            response_codes::REVERSAL_DECLINED,
        )?;

        debug!("冲正请求处理完成");
        Ok(MessageHandleResult {
            response_msg,
            success: true,
        })
    }

    fn supported_message_type(&self) -> &'static str {
        message_types::REVERSAL_REQUEST
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_reversal_handler_creation() {
        let handler = ReversalHandler::new();
        assert_eq!(handler.supported_message_type(), "1420");
    }
}